﻿<?php
/**
 * 命令行具体执行程序
 * 1)必须放置于cli/script目录下
 * 2)文件名必须为小写的cli_xxxx.php
 * 3)类名必须为Cli_Xxxx格式
 * 4)必须包含一个run($args)方法,$args为自动转换后的命令行参数
 *
 * 调用方法:
 * <code>
 * cli script=demo username=sharmy
 * </code>
 *
 * 命令行应用示例
 *
 * @author Chengtian.Hu<chengtian.hu@gmail.com>
 */

class Cli_autobg{
    public function run($args){
    		
    		set_time_limit(0);
    		
	    		 #monthcard
	    		 	 $cur_data = date('Y-m-d');
	    		 	 $data = QDB::getConn()->execute("select * from acct_bgpay where payday <='{$cur_data}' and process = 1 order by id asc ")->fetchAll();
		    		
		    		foreach ($data as $k=>$model)
		    		{
			    		if (isset($model['id']))
			    		{
			    				$statusSql = 'update voip.acct_bgpay set process=2 where id='.intval($model['id']);
								QDB::getConn()->execute($statusSql);
								Helper_Common::log($statusSql,__CLASS__);
								Helper_Common::log('Begin autobg task,ID:'.$model['id'].',acctname:'.$model['acctname'].' Data:'.print_r($model,true),__CLASS__);
				    			
				    			$updateSql = "UPDATE voip.acct_user set balance =balance- ".abs($model['paymoney']).' where id = '.intval($model['acct_id']);
				    			QDB::getConn()->execute($updateSql);
				    			Helper_Common::log($updateSql,__CLASS__);
				    			$insertLog = "insert into voip.acct_bgpaylog(admin_id,agent_id,acct_id,acctname,paymoney,created,class) values({$model['admin_id']},{$model['agent_id']},{$model['acct_id']},'{$model['acctname']}',{$model['paymoney']},unix_timestamp(now()),'monthcard')";
				    			QDB::getConn()->execute($insertLog);
				    			Helper_Common::log($insertLog,__CLASS__);
				    			$changeSql = 'update voip.acct_bgpay set process=1,payday=DATE_ADD(curdate(), INTERVAL 1 MONTH),updatetime=unix_timestamp(now()) where id='.intval($model['id']);
				    			QDB::getConn()->execute($changeSql);
				    			Helper_Common::log($changeSql,__CLASS__);
			    		}
		    		}
		    		#vip
		    		$data = QDB::getConn()->execute("SELECT * FROM acct_uservip where expdate <='{$cur_data}' and process = 1 order by id asc")->fetchAll();
		    		
		    		foreach($data as $k=>$model)
		    		{
		    			
 		    			$statusSql = 'delete from  voip.acct_uservip where id='.intval($model['id']);
 		    			QDB::getConn()->execute($statusSql);
 		    			Helper_Common::log($statusSql,__CLASS__);
		    			Helper_Common::log('Begin vip task,ID:'.$model['id'].',acctname:'.$model['acctname'].' Data:'.print_r($model,true),__CLASS__);
		    			#关闭
		    			
		    			//QDB::getConn()->execute("call fscall.acct_vippay('{$model['acctname']}',1)");
		    			#开通
		    			QDB::getConn()->execute("call fscall.acct_vippay('{$model['acctname']}',0)");
		    			QDB::getConn()->close();
// 		    			$updateSql = "UPDATE voip.acct_user set balance =balance- ".abs($model['money']).' where id = '.intval($model['acct_id']). " and acctname ='".$model['acctname']."' ";
// 		    			QDB::getConn()->execute($updateSql);
// 		    			Helper_Common::log($updateSql);
// 		    			$insertLog = "insert into voip.acct_bgpaylog(admin_id,agent_id,acct_id,acctname,paymoney,created,class) values({$model['admin_id']},{$model['agent_id']},{$model['acct_id']},'{$model['acctname']}',{$model['money']},unix_timestamp(now()),'vip')";
// 		    			QDB::getConn()->execute($insertLog);
// 		    			Helper_Common::log($insertLog);
// 		    			$changeSql = 'update voip.acct_uservip set process=1,expdate=DATE_ADD(curdate(), INTERVAL 1 MONTH),updatetime=unix_timestamp(now()) where id='.intval($model['id']);
// 		    			QDB::getConn()->execute($changeSql);
// 		    			Helper_Common::log($changeSql);
		    		}
		    		#delaymoney
		    		Helper_Common::log('begin delaymoney',__CLASS__);
		    		$data = QDB::getConn()->execute("SELECT * FROM acct_delaymoney WHERE created >= ".strtotime('-3 day',strtotime($cur_data))." and status = 0 order by id asc")->fetchAll();
					foreach ($data as $k=>$model)
					{
						$acct = Acct::find('id = ?',$model['agent_id'])->getOne();
						if ($acct->id())
						{
							if($acct->balance < 0)
							{
								Helper_Common::log('delaymoney:agent:'.$acct->acctname.' balance:'.$acct->balance,__CLASS__);
							}else{
								$updateSql = "update voip.acct set balance = balance - ".abs($model['money']).' where id = '.intval($model['agent_id']);								
								Helper_Common::log($updateSql,__CLASS__);
								QDB::getConn()->execute($updateSql);
								QDB::getConn()->execute('update voip.acct_delaymoney set status = 1,updated = unix_timestamp(now()) where id = '.intval($model['id']).' and status = 0');
							}
						}else{
							Helper_Common::log('delaymoney:not find agent',__CLASS__);
						}
					}
    }
}